package stack;

/**
 * 计算器demo(利用栈实现)
 */
public class Calculator {
    public static void main(String[] args) {
        String expression = "3+2*6-2+100*100000/10+100+982736/122";
        //创建两个栈，数栈和符号栈
        ArryStack2 numStack = new ArryStack2(100);
        ArryStack2 operStack = new ArryStack2(100);
        //定义相关变量
        int index = 0; //用于扫描
        int num1 = 0;
        int num2  = 0;
        int oper = 0;
        int res = 0;
        char ch = ' '; //每次扫描到的运算符保存到ch中
        String keepNum = "";  //用于拼接多位数

        //开始循环扫描
        while (true){
            //先得到表达式里面的每一个字符
            ch = expression.substring(index,index+1).charAt(0);
            //判断ch是什么，然后做相应的处理
            if(operStack.isOper(ch)){ //如果是运算符
                if(!operStack.isEmpy()){
                    //如果符号栈不为空，那么就需要比较优先级
                    if(operStack.priority(ch) <= operStack.priority(operStack.peek())){
                        //当前优先级比栈顶符号优先级低
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1,num2,oper);
                        //把运算结果入数栈
                        numStack.push(res);
                        //把当前这个操作符入符号栈
                        operStack.push(ch);
                    }else {
                        //当前优先级比栈顶符号优先级高
                        operStack.push(ch);
                    }

                }else{
                    //如果符号栈不为空，直接入栈
                    operStack.push(ch);
                }
            }else{
                //如果是数（只是个位数），那么就直接入栈
                //numStack.push(ch-48);  //ascll表中字符和实际数栈刚好相差48
                //需要处理多位数，如果后面是运算符，那么就直接入栈
                //如果是数字，那么就需要继续进行判断

                keepNum += ch;

                //如果ch已经是表达式的最后一位，那么就直接入栈
                if(index == expression.length()-1){
                    numStack.push(Integer.parseInt(keepNum));
                }else {
                    //判断下一个字符是不是数字,如果是，那么就继续扫描
                    if(operStack.isOper(expression.substring(index+1,index+2).charAt(0))){
                        numStack.push(Integer.parseInt(keepNum));
                        //需要把keepNum清空
                        keepNum = "";
                    }
                }
            }

            //让index+1 ,判断是否扫描到最后
            index++;
            if(index >=expression.length()){
                break;
            }
        }

        //当表达式扫描完毕后，顺序的从数栈和符号栈中pop出相应的值进行运算
        while (true){
            //如果符号栈为空，则计算到最后结果
            if(operStack.isEmpy()){
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1,num2,oper);
            //把运算结果入数栈
            numStack.push(res);

        }
        System.out.printf("表达式 %s = %d",expression,numStack.pop());
    }
}



//先创建一个栈
//定义一个类表示栈
class ArryStack2{
    private int maxSize;  //栈的大小
    private int[] stack;  //数组模拟栈，数据存放在数组中
    private int top = -1; //初始化为-1

    //构造器
    public ArryStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];  //数组要初始化
    }

    //可以返回当前栈顶的值（只是看一眼，不是pop）
    public int peek(){
        return stack[top];
    }


    //栈满
    public boolean isFull(){
        return top == maxSize-1;
    }

    //栈空
    public boolean isEmpy(){
        return top == -1;
    }

    //入栈
    public void push(int value){
        //判断栈是不是满了
        if(isFull()){
            System.out.println("栈满了");
            return;
        }
        top++;
        stack[top] = value;
    }

    //出栈
    public int pop(){
        //判断栈是不是空
        if(isEmpy()){
            //抛出异常
            throw new RuntimeException("栈空");
        }
        int vaule = stack[top];
        top--;
        return vaule;
    }

    //显示栈(遍历栈)，从上往下显示
    public void list(){
        if(isEmpy()){
            System.out.println("栈空，没有数据");
        }
        for(int i = top; i>=0 ; i--){
            System.out.printf("stack[%d] = %d \n",i,stack[i]);
        }
    }

    //返回运算符的优先级
    //优先级使用数字表示，数字越大，优先级越高
    public int priority(int oper){
        if(oper == '*' || oper == '/'){
            return 1;
        }else if(oper == '+' || oper =='-'){
            return 0;
        }else{
            return -1;  //表示目前的表达式只有加减乘除
        }
    }

    //判断是不是一个运算符
    public boolean isOper(char val){
        return val=='+' || val=='-' || val=='*' || val=='/';
    }

    //计算方法
    public int cal(int num1, int num2 ,int oper){
        int res = 0; //用于存放结果
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;

            case '-':
                res = num2 - num1;
                break;

            case '*':
                res = num1 * num2;
                break;

            case '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;
    }
}
